package com.cpfmat.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cpfmat.common.CommonStringUtils;
import com.cpfmat.common.LogUtils;
import com.cpfmat.entity.SystemUser;
import com.cpfmat.service.ISystemUserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangbin
 * @since 2018-07-30
 */
@Controller
public class SystemUserController {
	@Autowired
	private ISystemUserService systemUserService;

	/**
	 * 生成初始化的默认管理员账户
	 * 
	 * @param key
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws NoSuchAlgorithmException
	 */
	@RequestMapping("/generateAdministrator")
	@ResponseBody
	public String generateAdministrator(String key) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		if ("cpfmat".equals(key)) {
			SystemUser user = new SystemUser();
			String uuid = CommonStringUtils.getUUID();
			String password = CommonStringUtils.md5("123");
			Date nowTime = new Date();

			user.setSeqId(uuid);
			user.setDelFlag(0);
			user.setLoginName("admin");
			user.setLoginPwd(password);
			user.setUpdateCounts(0);
			user.setUpdateTime(nowTime);
			user.setUserName("军民网站后台管理员");
			user.insert();
			return "GENERATE SUCCESS!";
		}
		return "ERROR KEY!";
	}

	@RequestMapping("/login/{user}")
	public ModelAndView login(@PathVariable("user") String loginName, String password, HttpServletRequest request)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		password = CommonStringUtils.md5(password);

		SystemUser user = systemUserService.checkLoing(loginName, password);

		if (user != null) {
			request.getSession().setAttribute("loginUser", user);
			return new ModelAndView("redirect:/manage/index");
		}
		else{
			ModelAndView view = new ModelAndView("/manage/homePage");
			
			view.addObject("loginError", true);
			return view;
		}
	}
	
	@RequestMapping("/logout")
	public ModelAndView logout(HttpSession session) {
		session.invalidate();
		
		ModelAndView view = new ModelAndView("/manage/homePage");
		return view;
	}
	
	@RequestMapping("/manage/user/detail/{seqId}")
	public ModelAndView userDeatil(@PathVariable("seqId") String seqId){
		ModelAndView view = new ModelAndView("/manage/user/user");
		SystemUser user = systemUserService.selectById(seqId);
		
		view.addObject("user", user);
		return view;
	}
	
	@RequestMapping("/manage/user/save")
	@ResponseBody
	public String saveUser(String loginName,String loginPwd,String userName) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		if(StringUtils.isBlank(userName)){
			return "UN_NP";
		}
		if(StringUtils.isBlank(userName)){
			return "LN_NP";
		}
		if(StringUtils.isBlank(userName)){
			return "LP_NP";
		}
		SystemUser user = new SystemUser();
		String uuid = CommonStringUtils.getUUID();
		String password = CommonStringUtils.md5(loginPwd);
		Date nowTime = new Date();
		try{
			user.setSeqId(uuid);
			user.setDelFlag(0);
			user.setLoginName(loginName);
			user.setLoginPwd(password);
			user.setUpdateCounts(0);
			user.setUpdateTime(nowTime);
			user.setUserName(loginName);
			user.insert();
			return "SUCCESS";
		} catch(Exception e){
			return "DM_ER";
		}
	}

	/**
	 * 根据主键更新用户
	 * 
	 * @param seqId
	 * @param loginName
	 * @param loginPwd
	 * @param userName
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws NoSuchAlgorithmException
	 */
	@RequestMapping("/manage/user/modify/{seqId}")
	@ResponseBody
	public String updateUser(@PathVariable("seqId") String seqId, @RequestParam(required = false) String loginName,
			@RequestParam(required = false) String loginPwd, @RequestParam(required = false) String userName)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		if (StringUtils.isBlank(seqId)) {
			return "ID_NP";
		} else {
			try{
				SystemUser user = new SystemUser();
				user.setSeqId(seqId);
				user.setLoginName(loginName);
				user.setUserName(userName);
				if (loginPwd != null) {
					user.setLoginPwd(CommonStringUtils.md5(loginPwd));
				}
	
				systemUserService.customUpdate(user);
	
				LogUtils.debug(user);
				return "SUCCESS";
			} catch(Exception e){
				return "DM_ER";
			}
		}
	}
}